import {message} from 'antd';
import axios from 'axios';
import store from '@/store';

export const fetchPlatform = axios.create({
    baseURL: 'http://localhost:8080/api/',
    timeout: 10000
});

fetchPlatform.interceptors.request.use(config => {
    const {headers} = config || {};
    !headers.has('Content-Type') && headers.set('Content-Type', 'application/json; charset=utf-8');
    !headers.has('Authorization') && headers.set('Authorization', `bearer ${store.getState().root.token}`);
    return config;
}, error => {
    return Promise.reject(error);
});

fetchPlatform.interceptors.response.use(response => {
    return response.data;
}, error => {
    message.error(error?.response?.data?.message || '请求失败');
    return Promise.reject(error?.response?.data);
});
